﻿// --------------------------------------------------------------------------------
//                              Entity Functions
// --------------------------------------------------------------------------------

var Entity =
{
    newEntity: function()
    {
        var url = new Url("../Crud/EntityEdit.aspx");
        url.addParameter("entityName", entityID);        
        goTo(url);
    },

    editEntity: function(entityName, entityID)
    {
        var url = new Url("../Crud/EntityEdit.aspx");
        url.addParameter("entityName", entityName);
        url.addParameter("entityID", entityID);
        goTo(url);
    },

    list: function(entityName)
    {
        var url = new Url("../Crud/EntityList.aspx");
        url.addParameter("entityName", entityName);
        goTo(url);
    },
    
    entitySaved :function(entityName)
    {
        info("Data was successfully saved", "Entity.list('" + entityName + "')");
    }    
}

var Customer =
{
    list: function()
    {
        Entity.list("Demo.Model.Customer, Demo.Model");
    },
    
    deleteEntities: function(ids)
    {
        var response = CustomerController.Delete(ids);
        
        if(response.error) {
            alert(response.error);
        }
        else {
            CustomGrid_Refresh("customGrid");
        }    
    }
}



// --------------------------------------------------------------------------------
//                              Common Functions
// --------------------------------------------------------------------------------

function actionExample(lastName, firstName)
{
    info("Example of an action.<br> Viewing information about customer <br> '" + lastName + ", " + firstName + "'");
}

function openSaveConfigurationWindow()
{
    OpenFloatingWindow("saveConfigurationWindow", true);
}

function openLoadConfigurationWindow()
{
    var response = GridConfigurationController.List();
    
    if(response.error) {
        alert(response.error);
    }
    else {
        var configs = response.value;
        var configsContainer = document.getElementById("configsContainer");
        var html = "<table>";
        
        for(var i=0; i<configs.length; i++) {
            html += "<tr><td onclick=\"loadGridConfiguration('"+configs[i]+"')\" class=\"label\" style=\"cursor:pointer\">" + configs[i] + "</td></tr>";
        }
        html += "</table>";
        
        configsContainer.innerHTML = html;
        
        OpenFloatingWindow("loadConfigurationWindow", true);
    }
}

function loadGridConfiguration(name)
{
    var response = GridConfigurationController.Load("customGrid", name);

    if(response.error) {
        alert(response.error);
    }
    else {
        CustomGrid_Refresh("customGrid");
        ConfigManager.refresh();
        
        HideFloatingWindow("loadConfigurationWindow");        
    }    
}

function saveGridConfiguration()
{
    var txtName = document.getElementById("txtName");
    var response = GridConfigurationController.Save("customGrid", txtName.value);
   
    if(response.error) {
        alert(response.error);
    }
    else {
        txtName.value = "";    
        HideFloatingWindow("saveConfigurationWindow");
    }
}